package org.sjs.dreamfly.biz.history

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.base.BaseActivity

class HistoryActivity : BaseActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var viewModel: HistoryViewModel

    override fun onInitViewModel() {
        viewModel = ViewModelProvider(this).get(HistoryViewModel::class.java)
    }
    override fun onBindViews() {
        recyclerView = findViewById(R.id.recycler_view)
    }

    override fun requireLayoutId(): Int {
        return R.layout.activity_history
    }

    override fun onInitViews() {
        recyclerView.adapter = object : RecyclerView.Adapter<HistoryItemViewHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): HistoryItemViewHolder {
                return HistoryItemViewHolder(LayoutInflater.from(this@HistoryActivity).inflate(R.layout.item_history, null))
            }

            override fun getItemCount(): Int {
                return viewModel.getHistoryList().size
            }

            override fun onBindViewHolder(holder: HistoryItemViewHolder, position: Int) {
                holder.bind(viewModel.getHistoryList()[position], position)
            }
        }
        recyclerView.layoutManager = GridLayoutManager(this, 3)
    }


    override fun onLoadData(savedInstanceState: Bundle?, newIntent: Intent?) {}

    @SuppressLint("NotifyDataSetChanged")
    override fun onResume() {
        super.onResume()
        mainScope.launch {
            viewModel.loadHistory()
            hideLoadingView()
            recyclerView.adapter?.notifyDataSetChanged()
        }
    }
}